# Mounts a merged base image and recreates the bootflags registry keys that were deleted by MS updates
# next steps, power off the maintenance target
# next steps, connect to PVS to create the merged disk
# next steps, promote vdisk to maintenance
# next steps, power on the maintenance target

write-host "Merged Base must not be powered on. Verify no devices are booted to the image before proceeding" -ForegroundColor Red
pause

function Show-Menu
 {
            param (
            [string]$Title = ‘PVS Sites’
            )
            cls
            Write-Host “================ $Title ================”
    
            Write-Host “1: Press ‘1’ for EastUS2.”
            Write-Host “2: Press ‘2’ for WestEU.”
            Write-Host “3: Press ‘3’ for NorthEU.”
            Write-Host “Q: Press ‘Q’ to quit.”
 }

function Fix-VDA {
Import-Module “C:\Program Files\Citrix\Provisioning Services Console\Citrix.PVS.SnapIn.dll”
Set-PvsConnection -Name $PVSserver
$DiskLocator = Get-pvsdisklocator -StoreName $PVSStore -SiteName $PVSSite


$directory = "\\$PVSserver\d$\store\2025"
#Set-Location $directory
$VHDXFiles = Get-PvsDiskVersion -DiskLocatorId $DiskLocator.DiskLocatorId | Where-Object { ( $_.Access -like '1' ) -and ( $_.type -like '4' ) } | Select-Object DiskFileName,Version
    
    $(for($i=0; $i -lt $VHDXFiles.Count; $i++){
        [PSCustomObject]@{
            'Disk Name'       = "${i}: $($VHDXFiles.DiskFileName[$i])"
            'Version' = $VHDXFiles.Version[$i]
                }
            }) | Out-Host

$input = Read-Host -Prompt "Select a file from the menu"
#Clear-Host
#$storepath = get-pvsstore -storename $store | select-object path
#Write-Host "Path to image: $storepath\$($VHDXFiles[$input])" -ForegroundColor Yellow

Write-Host "You have just selected: $directory\$($VHDXFiles[$input].diskfilename)" -ForegroundColor Yellow

Write-Host "Mounting VHDX" -ForegroundColor Yellow
$VolumesVHDX = Mount-DiskImage -ImagePath "$directory\$($VHDXFiles[$input].diskfilename)" -PassThru | Get-Disk | Get-Partition | Get-Volume
$dl = $VolumesVHDX.DriveLetter
write-host "image is mounted to: $dl"

$String='Set-Partition -DriveLetter ' + $dl + ' -NewDriveLetter P -Confirm:$false -ErrorAction SilentlyContinue'
$String | Invoke-Expression

Write-Host "Mounting Registry Hive" -ForegroundColor Yellow
$HivePath = "P:\Windows\System32\config\System"
reg load "HKLM\vdisk" $HivePath
$RegistryKey = "HKLM:\vdisk\ControlSet001\Services\netvsc"
$RegistryValue = "BootFlags"
$RegistryValueType = "DWord"
$RegistryValueData = 1

Write-Host "Creating:: -Path $RegistryKey -Name $RegistryValue -PropertyType $RegistryValueType -Value $RegistryValueData " -ForegroundColor Green

$Result = New-ItemProperty -Path $RegistryKey -Name $RegistryValue -PropertyType $RegistryValueType -Value $RegistryValueData -Force
# Cleanup (to prevent access denied issue unloading the registry hive)
Remove-Variable Result
Get-Variable Registry* | Remove-Variable
[gc]::collect()
Start-Sleep -Seconds 5

# Unload the registry hive
Write-host "Unloading the registry hive"
Set-Location "c:\scripts"
reg unload "HKLM\vdisk"
Dismount-DiskImage -ImagePath "$directory\$($VHDXFiles[$input].diskfilename)"  }

 do
 {
            Show-Menu
            $input = Read-Host “Please make a selection”
            switch ($input)
            {
            ‘1’ {
                 ‘You chose site: EastUS2’
                 $PVSserver = "01600"
                 $PVSstore = "2025"
                 $PVSsite = "US East2"
                 Fix-VDA
            } ‘2’ {
                 ‘You chose site: WestEU2’
                 $PVSserver = "01640"
                 $PVSstore = "2025"
                 $PVSsite = "West Europe"
                 Fix-VDA
            } ‘3’ {
                 ‘You chose site: NorthEU’
                 $PVSserver = "01724"
                 $PVSstore = "2025"
                 $PVSsite = "North Europe"
                 Fix-VDA
            } ‘q’ {
                 return
            }
            }
            #pause
 }
 until ($input -eq ‘q’)